package thinkingInJava;

/**
 * 类部类
 * 
 * @author jieji
 *
 */
public class Learn10 {
	public static void main(String[] args) {

	}
	
	//迭代器
	public static void selector(){
		Out out=new Out(10);
		out.add(1);
		out.add(2);
		out.add(3);
		out.add(4);
		out.add(5);
		out.add(6);
		out.add(7);
		out.add(8);
		out.add(9);
		out.add(10);
		Selector selector=out.selector();
		while(!selector.end()){
			System.out.println("current:"+selector.current());
			selector.next();
		}	
	}
}

/**
 * 迭代器设计模式
 * 类部类实现
 *
 */
interface Selector {
	//判断是不是最后一个
	boolean end();
	//拿到当前对象
	Object current();
	//计数加1拿下一个
	void next();
}

class Out {
	private Object[] objs;
	private Integer next = 0;

	public Out(Integer size) {
		objs = new Object[size];
	}

	public void add(Object obj) {
		if (next < objs.length) {
			objs[next++] = obj;
		}
	}

	public Selector selector() {
		return new Inner();
	}

	private class Inner implements Selector {
		private Integer i = 0;

		@Override
		public boolean end() {
			return i == objs.length;
		}

		@Override
		public Object current() {

			return objs[i];
		}

		@Override
		public void next() {
			if (i < objs.length) {
				i++;
			}
		}
	}
}
